<?php
namespace Kxdrmobile\Model;
use Think\Model;
class MainUserAddressModel extends Model{
	protected $trueTableName = 'sm_user_address'; 
	public $sid;
	public $uid;
	public $save_data;
	public $id;
	public function getUserAddress(){
		$whe["sid"]=$this->sid;
		$whe["kxdr_uid"]=$this->uid;
		return $this->where($whe)->getField("`id`,`nationality`,`province`,`city`,`city_part`,`address`,`name`,`tel`");
	}
	public function getUserDefaultAddress(){
		$whe["sid"]=$this->sid;
		$whe["kxdr_uid"]=$this->uid;
		$whe["is_def"]=true;
		return $this->where($whe)->limit(1)->getField("`id`,`nationality`,`province`,`city`,`city_part`,`address`,`name`,`tel`");
	}
	public function getUserDefaultAddressID(){
		$whe["sid"]=$this->sid;
		$whe["kxdr_uid"]=$this->uid;
		$whe["is_def"]=true;
		return $this->where($whe)->limit(1)->getField("`id`");
	}
	public function setDefault(){
		$whe["sid"]=$this->sid;
		$whe["kxdr_uid"]=$this->uid;
		$this->where($whe)->save(Array("is_def"=>false));
		
		$whe["id"]=$this->id;
		return $this->where(Array("id"=>$this->id))->save(Array("is_def"=>true));
	}
	public function saveAddress(){
		$address = $this->getUserDefaultAddressID();
		if($address){
			$this->save_data["id"]=$address;
			$s = $this->save($this->save_data);
			if($s===false){
				$re["msg"]="保存失败！请重新输入地址信息";
			}elseif($s===0){
				$re["msg"]="您没有修改地址！";
			}else{
				$re["msg"]="您已成功修改地址！";
			}
		}else{
			$re["hasError"] = !$this->add($this->save_data);
			$re["msg"]="您已成功添加地址！";
		}
		return $re;
	}
}